export default ({
	method = 'GET',
	url = '',
	data = {},
	headers = {
		'Content-Type': 'application/json; charset=UTF-8',
	},
} = {}) => {
	if (!url) return Promise.reject('url 不能为空')

	const xhr = new XMLHttpRequest()
	xhr.open(method, url)
	xhr.timeout = 5000
	Object.keys(headers).forEach(item => xhr.setRequestHeader(item, headers[item]))
	
	return new Promise((resolve, reject) => {
		xhr.onreadystatechange = () => xhr.readyState === 4 && (xhr.status === 200 ? resolve({status: 200, data: xhr.responseText}) : reject({status: xhr.status, data: xhr.statusText}))

		xhr.send(data)
	})
}
